<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="机构编号" prop="tenantNo">
      <el-input v-model="dataForm.tenantNo" placeholder="机构编号"></el-input>
    </el-form-item>
    <el-form-item label="机构简称" prop="tenantShortName">
      <el-input v-model="dataForm.tenantShortName" placeholder="机构简称"></el-input>
    </el-form-item>
    <el-form-item label="机构全称" prop="tenantName">
      <el-input v-model="dataForm.tenantName" placeholder="机构全称"></el-input>
    </el-form-item>
    <el-form-item label="联系人" prop="owner">
      <el-input v-model="dataForm.owner" placeholder="联系人"></el-input>
    </el-form-item>
    <el-form-item label="联系方式" prop="tel">
      <el-input v-model="dataForm.tel" placeholder="联系方式"></el-input>
    </el-form-item>
    <el-form-item label="公司logo" prop="logo">
      <el-input v-model="dataForm.logo" placeholder="公司logo"></el-input>
    </el-form-item>
    <el-form-item label="备注信息" prop="remark">
      <el-input v-model="dataForm.remark" placeholder="备注信息"></el-input>
    </el-form-item>
    <el-form-item label="状态" prop="status">
      <el-input v-model="dataForm.status" placeholder="状态"></el-input>
    </el-form-item>
    <el-form-item label="创建用户ID" prop="createUserId">
      <el-input v-model="dataForm.createUserId" placeholder="创建用户ID"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          tenantId: 0,
          tenantNo: '',
          tenantShortName: '',
          tenantName: '',
          owner: '',
          tel: '',
          logo: '',
          remark: '',
          status: '',
          createUserId: '',
          createTime: ''
        },
        dataRule: {
          tenantNo: [
            { required: true, message: '机构编号不能为空', trigger: 'blur' }
          ],
          tenantShortName: [
            { required: true, message: '机构简称不能为空', trigger: 'blur' }
          ],
          tenantName: [
            { required: true, message: '机构全称不能为空', trigger: 'blur' }
          ],
          owner: [
            { required: true, message: '联系人不能为空', trigger: 'blur' }
          ],
          tel: [
            { required: true, message: '联系方式不能为空', trigger: 'blur' }
          ],
          logo: [
            { required: true, message: '公司logo不能为空', trigger: 'blur' }
          ],
          remark: [
            { required: true, message: '备注信息不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '状态不能为空', trigger: 'blur' }
          ],
          createUserId: [
            { required: true, message: '创建用户ID不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.tenantId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.tenantId) {
            this.$http({
              url: this.$http.adornUrl(`/tenant/tenant/info/${this.dataForm.tenantId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.tenantNo = data.tenant.tenantNo
                this.dataForm.tenantShortName = data.tenant.tenantShortName
                this.dataForm.tenantName = data.tenant.tenantName
                this.dataForm.owner = data.tenant.owner
                this.dataForm.tel = data.tenant.tel
                this.dataForm.logo = data.tenant.logo
                this.dataForm.remark = data.tenant.remark
                this.dataForm.status = data.tenant.status
                this.dataForm.createUserId = data.tenant.createUserId
                this.dataForm.createTime = data.tenant.createTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/tenant/tenant/${!this.dataForm.tenantId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'tenantId': this.dataForm.tenantId || undefined,
                'tenantNo': this.dataForm.tenantNo,
                'tenantShortName': this.dataForm.tenantShortName,
                'tenantName': this.dataForm.tenantName,
                'owner': this.dataForm.owner,
                'tel': this.dataForm.tel,
                'logo': this.dataForm.logo,
                'remark': this.dataForm.remark,
                'status': this.dataForm.status,
                'createUserId': this.dataForm.createUserId,
                'createTime': this.dataForm.createTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
